package com.example.springbootdemo09.service.impl;

import com.example.springbootdemo09.domain.User;
import com.example.springbootdemo09.exception.BusinessException;
import com.example.springbootdemo09.mapper.UserMapper;
import com.example.springbootdemo09.service.IUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;

@Service
public class UserServiceImpl implements IUserService {
 public static Map<String,Object> map= new ConcurrentHashMap();

    @Autowired
    private UserMapper urm;
    @Override
    public String findByUsername(User user) {
        String userName = user.getUsername();
        String password = user.getPassword();
        if(StringUtils.isBlank(userName)||StringUtils.isBlank(password)){
            throw new BusinessException("信息不能为空");
        }
        User byUsername = urm.findByUsername(userName);
        if (byUsername==null){
            throw new BusinessException("用户不存在");
        }else {
            if (!password.equals(byUsername.getPassword())){
          throw new BusinessException("密码错误");
            }else {
                String token = UUID.randomUUID().toString();
                map.put(token,byUsername);
                return token;
            }
        }
    }
}
